<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 200px;
      height: 3000px;
      border: 1px solid salmon;
    }
    .back-top {
      position: fixed;
      right: 30px;
      bottom: 50px;
      border: 1px solid salmon;
      padding: 10px;
      display: none;
    }
  </style>
</head>
<body>
  <div class="box box1">1</div>
  <!-- 当 页面滚出去 300像素，出现 下面的 按钮 -->
  <div class="back-top">返回顶部</div>
</body>
</html>
<script>
// 英文： source  - 源； src
  // behavior - 行为、习惯
  // smooth - 平滑/丝滑
var box1 = document.querySelector('.box1'); // source
var domBackTop = document.querySelector('.back-top');
window.onscroll = function() {
  var top = document.body.scrollTop || document.documentElement.scrollTop;
  if (top > 300) {
    domBackTop.style.display = 'block';
  } else {
    domBackTop.style.display = 'none';
  }
}

domBackTop.onclick = function() {
  console.log('click');
  // 基础版本
  // document.body.scrollTop = 0;
  // document.documentElement.scrollTop = 0;

  // 优化版本
  window.scrollTo({
    top: 20,
    behavior: 'smooth'
  });

}

</script>